import json

import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import execjs

with open("sign加密.js", "r", encoding="utf-8") as f:
    data = f.read()

js = execjs.compile(data)
word = input("请输入要翻译的单词:")
result = js.call("result", word)
headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Acs-Token": "1703563265967_1703598530819_aEyMHv9wvIg9oJOqj+C8q+JCE8rRHtiJ4vQGugxTtlcI9yDE8nw35S4mnAYwTRbIecoG0tsEZTdRBiqK2TjtvH183FMyPrAhA/S4hcCUrPLkv9LRg9xRtlXELzjZNpAgcUkw9WkkhaIvnEo4kjogwFWFhR6SNCRuutt9MkUa2a9nZXIVIz5zjdA1SylE5kNsDj8VUk1fY0jZJIY8456QtV1FI7rl70K6vNytNGxJjUBiqQeM5uYoFv5QlmyhATa6u6GNMLIVRu39EHqlFpsTgD4s9rSvGm432jeHYELCvAYnje7Zhud2SbH2NOngT8ixNpm5dJzECcfPIz4F9UpfHn3NkNe0enPUn85NRMUvzyR+1jFMtgoIJq77rUkh3/t+ylfLkPCNe93A8Mm0xcqAlFHgpSki5vbWH1X82NZxynhxlRybkwHl8a0iBB/uTXfcD+SqRVRYTIh2/WbHIvOhkLtsDHjWD97iA+R2jhbBH+A=",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://fanyi.baidu.com",
    "Referer": "https://fanyi.baidu.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "BIDUPSID": "A0634593383644135F226D746466406B",
    "PSTM": "1694745134",
    "BAIDUID": "A0634593383644138DC2EAE6481A5641:FG=1",
    "REALTIME_TRANS_SWITCH": "1",
    "FANYI_WORD_SWITCH": "1",
    "HISTORY_SWITCH": "1",
    "SOUND_SPD_SWITCH": "1",
    "SOUND_PREFER_SWITCH": "1",
    "APPGUIDE_10_6_6": "1",
    "H_WISE_SIDS": "110085_275733_259642_280650_281810_281867_281888_281994_283633_281704_283721_283886_284024_278388_284280_284284_284276_282815_284216_284786_284802_284883_284878_284889_278414_282485_284812_285064_281695_284538_285647",
    "H_WISE_SIDS_BFESS": "110085_275733_259642_280650_281810_281867_281888_281994_283633_281704_283721_283886_284024_278388_284280_284284_284276_282815_284216_284786_284802_284883_284878_284889_278414_282485_284812_285064_281695_284538_285647",
    "BDUSS": "nIwUUtjN054WXJkSVFrWUNVMTBuVUtBYmJXQ3U1WTFBT3FiaXo3SU4zVHJzNmRsSVFBQUFBJCQAAAAAAAAAAAEAAAClxHVGxdrNxbOkb2wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOsmgGXrJoBlT",
    "BDUSS_BFESS": "nIwUUtjN054WXJkSVFrWUNVMTBuVUtBYmJXQ3U1WTFBT3FiaXo3SU4zVHJzNmRsSVFBQUFBJCQAAAAAAAAAAAEAAAClxHVGxdrNxbOkb2wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOsmgGXrJoBlT",
    "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
    "H_PS_PSSID": "39934_39936_39932_39946_39939_39974_39990",
    "BAIDUID_BFESS": "A0634593383644138DC2EAE6481A5641:FG=1",
    "PSINO": "6",
    "delPer": "0",
    "Hm_lvt_64ecd82404c51e03dc91cb9e8c025574": "1702735129,1703224677,1703491716,1703577772",
    "BAIDU_WISE_UID": "wapp_1703579339460_473",
    "arialoadData": "false",
    "ZFY": "gGhFCLU8uLjIKlANSsq3SI7:BCCTqP0yIQGiu5tbJy1s:C",
    "RT": "\"z=1&dm=baidu.com&si=1ea95897-147a-4d7a-9f29-9a69c4b228f2&ss=lqm35iae&sl=6&tt=d1o&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=423d&ul=1qt9y&hd=1qtej\"",
    "ab_sr": "1.0.1_Y2Q3NzQ1MzhkMjI2NzA3YWUyYjg3ZGUzY2ExZGY2Y2IyN2YyOWJlY2FjZDQwMzdkMDRiMWI4OWRkZGMxNjFmYWE5MTE5MDE0MGJkMjRhOGEwNjZiMDQ0ZWNiMTFkMjAxMjg0ZGJjYTZiOTExNTJhYTM1MDQ2ZjA3MGJmZGRhNjBiN2FiN2ZjYWQ5ZjgwZTg1Y2RiMzk2YzE2MTA5MzIzNjNhOWY5MzFhZmNkMzJhZDcyMDQyMDQxNmJhOWFlZDU5",
    "Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574": "1703598051",
    "BA_HECTOR": "840kag252h008184ag0g85a53qlc3g1iollvf1t"
}
url = "https://fanyi.baidu.com/v2transapi"
params = {
    "from": "en",
    "to": "zh"
}
data = {
    "from": "en",
    "to": "zh",
    "query": word,
    "transtype": "realtime",
    "simple_means_flag": "3",
    "sign": result["sign"],
    "token": "cfca548b584d513733485c47b7e1602a",
    "domain": "common",
    "ts": result["ts"]
}
response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data)

json_data = json.loads(response.text)
word_result = json_data["dict_result"]["simple_means"]["word_means"]
print(word_result)
